package com.group01.community.config;

import com.group01.community.constant.Constants;
import com.group01.community.entity.vo.SysUserTokenVo;
import com.group01.community.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author : guoyuan
 * date : @since 2025-05-01
 */
@Slf4j
public class LoginFailureHandle implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        //记录失败信息
        AsyncFactory.recordLogininfor(httpServletRequest.getParameter("username"),
                Constants.LOGIN_FAIL,
                ResponseStatus.LOGIN_ERROR.getMessage());
        ResponseResult.write(httpServletResponse, ResponseResult.error(ResponseStatus.LOGIN_ERROR));
    }
}